package com.hs.carrental.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hs.carrental.dto.car.CarsPageDto;
import com.hs.carrental.dto.car.SaveCarDto;
import com.hs.carrental.entity.Car;
import com.hs.carrental.mapper.CarMapper;
import com.hs.carrental.service.ICarService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hs.carrental.util.Helper;
import com.hs.carrental.vo.car.CarPageVo;
import net.sf.jsqlparser.expression.operators.relational.MinorThanEquals;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lyy
 * @since 2021-03-17
 */
@Service
public class CarServiceImpl extends ServiceImpl<CarMapper, Car> implements ICarService {

    @Autowired
    private CarMapper carMapper;

    /**
     * 新增或编辑车辆信息
     * @param saveCarDto
     * @return
     */
    @Override
    public boolean save(SaveCarDto saveCarDto) {
        boolean save = false;
        Car car = new Car();
        BeanUtils.copyProperties(saveCarDto,car);
        if (Helper.isEmpty(saveCarDto.getCarId())){
            //新增
            save = this.save(car);
        } else {
            //编辑
            save = this.updateById(car);
        }
        return save;
    }

    @Override
    public IPage<CarPageVo> getCarsPage(CarsPageDto carsPageDto) {
        //新建分页对象
        Page<Car> page = new Page<>(carsPageDto.getPageNum(),carsPageDto.getPageSize());
        //分页查询
        IPage<CarPageVo> carsPage = carMapper.getCarsPage(page, carsPageDto.getCarName(), carsPageDto.getCarNumber(),
                carsPageDto.getState(), carsPageDto.getStoreId(),carsPageDto.getCarSearch());
        return carsPage;
    }

    @Override
    public boolean updateTo0(Integer carId) {
        UpdateWrapper<Car> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("state",0).eq("carId",carId);
        return this.update(updateWrapper);
    }
}
